﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYElectricKernel;

namespace HYRElectric.LightningCalculation
{
	public class LightningCalculationManager
	{
		public LightningCalculationManager(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.m_Doc = this.m_Revit.Application.ActiveUIDocument.Document;
		}

		public Result ProfileCalculateByFormulaMethod(ref double length, ref double width, ref double height, ref string message)
		{
			Level levelByElevation = this.GetLevelByElevation(0.0);
			LevelProfileAnalyzer levelProfileAnalyzer = new LevelProfileAnalyzer(this.m_Revit);
			LevelProfiles levelProfiles = levelProfileAnalyzer.GetLevelProfiles(levelByElevation, true);
			if (levelProfiles.Profiles.Count == 0)
			{
				message = "未找到有效分析包围矩形";
				return Autodesk.Revit.UI.Result.Failed;
			}
			height = levelProfileAnalyzer.GetBuildingMaxHeight();
			List<XYZ> list = levelProfileAnalyzer.CetMinAreaRect(levelProfiles, ref length, ref width);
			if (list.Count != 4)
			{
				message = "未找到有效分析包围矩形";
				return Autodesk.Revit.UI.Result.Failed;
			}
			Transaction transaction = null;
			try
			{
				transaction = new Transaction(this.m_Doc);
				transaction.Start("show interim");
				ReferenceCurveDrawingKit.DrawBox(this.m_Revit.Application, list[0], list[1], list[2], list[3], height);
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction != null)
				{
					transaction.RollBack();
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result ProfileCalculateByGraphicMethod(List<Level> levelList, ref LevelProfiles unionLevelProfiles, ref string message)
		{
			LevelProfileAnalyzer levelProfileAnalyzer = new LevelProfileAnalyzer(this.m_Revit);
			LevelProfiles levelProfiles = new LevelProfiles(0.0);
			foreach (Level level in levelList)
			{
				double offset = LightningCalculator.CalculateOffset(level.Elevation);
				LevelProfiles levelProfiles2 = levelProfileAnalyzer.GetLevelProfiles(level, false);
				if (levelProfiles2.Profiles.Count != 0)
				{
					foreach (Profile profile in levelProfileAnalyzer.GetOffsetProfiles(levelProfiles2, offset).Profiles)
					{
						levelProfiles.AddProfile(profile);
					}
				}
			}
			if (levelProfiles.Profiles.Count == 0)
			{
				message = "未找到有效分析轮廓";
				return Autodesk.Revit.UI.Result.Failed;
			}
			unionLevelProfiles = levelProfileAnalyzer.GetUnionProfiles(levelProfiles);
			Transaction transaction = null;
			try
			{
				transaction = new Transaction(this.m_Doc);
				transaction.Start("show interim");
				ReferenceCurveDrawingKit.DrawProfile(this.m_Revit.Application, unionLevelProfiles);
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction != null)
				{
					transaction.RollBack();
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Level GetLevelByElevation(double elevation)
		{
			List<Level> sortLevels = Common.GetSortLevels(this.m_Doc, double.MinValue, double.MaxValue);
			Level result = null;
			double val = double.MaxValue;
			foreach (Level level in sortLevels)
			{
				double num = Math.Abs(elevation - level.Elevation);
				if (Geometry.LessThan(num, val))
				{
					val = num;
					result = level;
				}
			}
			return result;
		}

		private ExternalCommandData m_Revit;

		private Document m_Doc;
	}
}
